using System;
using System.Collections.Generic;
using UnityEngine;

namespace YPFramework
{
    [Serializable]
    public class UIData
    {
        public string key;
        public UnityEngine.Object gameObject;
    }
    
    public class UIBase: MonoBehaviour, ISerializationCallbackReceiver
    {
        public UILayerType layerType = UILayerType.Middle;
        
        [HideInInspector] public List<UIData> list = new List<UIData>();
        private readonly Dictionary<string, UnityEngine.Object> _referenceDic = new Dictionary<string, UnityEngine.Object>();
        
        public T GetReference<T>(string key) where T : UnityEngine.Object
        {
            return _referenceDic.TryGetValue(key, out var obj) ? (T)obj : null;
        }
        
        public bool ContainReference(string key)
        {
            return _referenceDic.ContainsKey(key);
        }
        
        public void AddReference<T>(string key, T t) where T : UnityEngine.Object
        {
            if (!_referenceDic.ContainsKey(key))
            {
                UIData data = new UIData();
                data.key = key;
                data.gameObject = t;
                list.Add(data);
            }
            else
                Debug.LogError("请勿重复添加!");
        }
        
        public void OnAfterDeserialize()
        {
            _referenceDic.Clear();
			
            foreach (var referenceCollectorData in list)
            {
                if (_referenceDic.ContainsKey(referenceCollectorData.key))
                {
                    continue;
                }
				
                _referenceDic.Add(referenceCollectorData.key, referenceCollectorData.gameObject);
            }
        }
		
        public void OnBeforeSerialize() { }

        /// <summary>
        /// 创建UI
        /// </summary>
        public void Create()
        {
            OnCreate();
            OnInit();
        }

        /// <summary>
        /// 打开UI
        /// </summary>
        public void Open()
        {
            transform.SetAsLastSibling();
            gameObject.SetActive(true);
            OnOpen();
            UpdateView();
        }

        /// <summary>
        /// 隐藏UI
        /// </summary>
        public void Hide()
        {
            OnHide();
            gameObject.SetActive(false);
        }


        /// <summary>
        /// 销毁UI时调用
        /// </summary>
        public void Dispose(float delayTime = 0)
        {
            Hide();
            OnDispose();
            Destroy(gameObject, delayTime);
        }

#region 生命周期
        /// <summary>
        /// 初始化UI时调用
        /// </summary>
        protected virtual void OnCreate(){ }
        
        /// <summary>
        /// UI绑定数据时调用
        /// </summary>
        protected virtual void OnBind(){ }

        /// <summary>
        /// 初始化UI时调用
        /// </summary>
        protected virtual void OnInit(){ }

        /// <summary>
        /// 打开UI时调用
        /// </summary>
        protected virtual void OnOpen(){ }

        /// <summary>
        /// 关闭UI时调用
        /// </summary>
        protected virtual void OnHide(){ }
        
        /// <summary>
        /// 销毁UI时调用
        /// </summary>
        protected virtual void OnDispose(){ }
#endregion

        /// <summary>
        /// 更新UI界面
        /// </summary>
        public virtual void UpdateView()
        {
            
        }
    }
}
